package com.xw.screen.capture.util;

import com.alibaba.fastjson.JSON;
import com.xw.screen.capture.entity.MultipartFileImpl;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.*;

/**
 * @author ：xiongwei
 * @date ：Created in 2020/11/22 17:39
 */
public class FileUpload {

    /**
     * 发送post请求远程服务器
     *
     * @param serviceUrl：远程服务器接口地址
     * @param filePath：文件存在地址
     * @param fileName：文件名称
     * @param json：参数
     * @return
     */
    public static String restTemplateTransferFilePost(String serviceUrl, String filePath, String fileName, JSON json) throws IOException {
       /*  //大文件时
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        //这是为tru时最大为10485760字节，文件过大会抛出java.io.IOException: Error writing to server，大文件设置为false
        requestFactory.setBufferRequestBody(false);
        RestTemplate restTemplate = new RestTemplate(requestFactory);*/
        //小文件时
        RestTemplate restTemplate = new RestTemplate();
        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("multipart/form-data");
        MediaType fileNameType = MediaType.parseMediaType("multipart/form-data");
        headers.setContentType(type);
        headers.add("fileName",fileName);
        String path = filePath + File.separator + fileName;
//        String fileMd5 = DigestUtils.md5Hex(new FileInputStream(new File(path)));
        // 建立需要上传的资源对象
        FileSystemResource fileSystemResource = new FileSystemResource(path);
        //设置请求体，注意是LinkedMultiValueMap
        MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
        // 指定上传文件所对应的字段及资源
        form.add("file", fileSystemResource);
        form.add("filename", fileName);
        form.add("parameterJson", json);
//        form.add("fileMd5", fileMd5);
        //用HttpEntity封装整个请求报文
        HttpEntity<MultiValueMap<String, Object>> files = new HttpEntity<>(form, headers);
        String s = restTemplate.postForObject(serviceUrl, files, String.class);
        return s;
    }

    /**
     * 发送post请求远程服务器
     *
     * @param serviceUrl：远程服务器接口地址
     * @param fileName：文件名称
     * @param json：参数
     * @return
     */
    public static String restTemplateTransferFilePost(String serviceUrl, MultipartFileImpl multipartFile, String fileName, JSON json) throws IOException {
        if (null == multipartFile){
            throw new IOException("inputStream 空了!!!");
        }
        //小文件时
        RestTemplate restTemplate = new RestTemplate();
        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("multipart/form-data");
        MediaType fileNameType = MediaType.parseMediaType("multipart/form-data");
        headers.setContentType(type);
        headers.add("fileName",fileName);
//        String fileMd5 = DigestUtils.md5Hex(new FileInputStream(new File(path)));
        // 建立需要上传的资源对象
        //设置请求体，注意是LinkedMultiValueMap
        MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
        // 指定上传文件所对应的字段及资源
        form.add("file", multipartFile);
        form.add("filename", fileName);
        form.add("parameterJson", json);
//        form.add("fileMd5", fileMd5);
        //用HttpEntity封装整个请求报文
        HttpEntity<MultiValueMap<String, Object>> files = new HttpEntity<>(form, headers);
        String s = restTemplate.postForObject(serviceUrl, files, String.class);
        return s;
    }

}
